

$(function() {
	$("#submit1").click(function(e) {
		e.preventDefault();
		var url = "http://ittalentsapi.bashibozuk.eu/users/login?X-GameID=102";
		
		var myData = {};
                myData.username = $('#username').val();
                myData.password = $('#password').val();
		
		$.ajax({
			async: false,
			cache: false,
			type: 'post',
			dataType: 'json',  // json...just for example sake
			data: myData,
			url: 'http://ittalentsapi.bashibozuk.eu/users/login?X-GameID=102',
			success: function (data) {
//				data = {
//					id: 1
//				};
				if (data.errors) {
                                        
					$('.errors').html('').css("color", "white");
					for(i in data.errors) {
						$('#errors_' + i).html(data.errors[i].join('<br>'));
					}
                                        $('#error').html("Error Invalid username or password");
                                        
				} else {
					$('.errors').html('');
					
					data = JSON.stringify(data);
					window.sessionStorage.setItem('myData', data);
					window.location.href = '../site/play.html';
					if (data.id) {
						$('#user').html('Sign out').css('color', 'fff');
					}
				}
//				console.log(data);
//				// retrieve a success/failure code from the server
//				if (myDate.username == data.username && uPass == data.password) {  // server returns a "1" for success
//					$('#errors').html('You have succesfully log in!');
//					console.log(data);
//					
//					//----------------------
//					
//					
//					
//					//-----------------------
//					
//					window.location = "game/game.html";
//					
//				} else {
//					// fail!
//					console.log('failed');
//					$('#errors').html('Please try again!');
//				}
//			},
//			error: function (XMLHttpRequest, textStatus, errorThrown) {
//				// something went wrong with the request
//				alert(XMLHttpRequest.responseText);
			}
		});
	});
});
